package com.trimps.steps.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hotfire.template.common.DateUtils;
import com.hotfire.template.common.Paginator;
import com.trimps.steps.pojo.Step;
import com.trimps.steps.pojo.WalkerGroup;
import com.trimps.steps.service.IStepService;
import com.trimps.steps.service.IWalkerGroupService;
import com.trimps.steps.service.IWalkerService;

@Controller
public class MainController {
	private static final Logger logger = Logger.getLogger(MainController.class);

    @Autowired
    private IWalkerGroupService walkerGroupService;
    @Autowired
    private IWalkerService walkerService;
    @Autowired
    private IStepService stepService;
    
    /**
     * 首页预览
     * @param mMap
     * @return
     */
	@RequestMapping(value = "main")
	public String main(ModelMap mMap) {
	    List<WalkerGroup> walkerGroups =  walkerGroupService.getAllWalkerGroups();
	    HashMap<Integer, List<Step>> stepMap = new HashMap<Integer, List<Step>>();
	    Paginator page = new Paginator();
	    for(WalkerGroup mg : walkerGroups) {
	        HashMap<String, String> queryMap = new HashMap<String, String>();
	        queryMap.put("stepDate", DateUtils.dateToString(new Date(new Date().getTime()-(24*60*60*1000)), "yyyy-MM-dd"));
	        queryMap.put("groupId", String.valueOf(mg.getId()));
	        queryMap.put("orderBy", "step_date DESC, step_count DESC ");
	        page.setShowItemCount(50);
	        
	        List<Step> steps = stepService.getSteps(page.getStart(), page.getOffset(), queryMap);
	        if(steps.size() > 0)
	            stepMap.put(mg.getId(), steps);
	    }
        mMap.addAttribute("stepMap", stepMap);
	    mMap.addAttribute("groups", walkerGroups);
	    
	    return "main";
	}

}
